import sys

sys.path.append('../')

from config import *


@app.route('/add_contact.do', methods = ['GET'])
def add_contact():
    if not 'username' in session:
        return redirect(url_for('login'))

    id = request.args.get('id', '').strip()

    if not id:
        resp = {
            'success': False,
            'msg': 'invalid resume id'
        }

        response = make_response(jsonify(resp))
        return response

    user = qushu_cv_match_user_coll.find_one({'uid': session['uid']})

    if 'contact_list' in user and id in user['contact_list']:
        resp = {
            'success': False,
            'msg': '后台客服正在为您寻找此份简历联系方式，请勿重复添加~!'
        }

        response = make_response(jsonify(resp))
        return response

    qushu_cv_match_user_coll.update_one({'uid': session['uid']}, {'$push': {'contact_list': id}})

    # 将此id插入联系人数据库中,等待后续通过人工或者自动化的方式获取联系方式
    if not qushu_contact_coll.find_one({'id': id}):
        qushu_contact_coll.insert_one({
            'id': id,
            'contact_information': ''
        })

    resp = {
        'success': True,
        'msg': '正在为您获取联系方式，请您稍后在【联系方式】板块查看!'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/code_by_user.do', methods = ['GET'])
def code_by_user():
    positions = json.loads(request.args.get('positions'))
    zhilian.hit_code(positions)

    resp = {
        'success': True
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/get_contact.do', methods = ['GET'])
def get_contact():
    if not 'username' in session:
        return redirect(url_for('login'))

    id = request.args.get('id', '').strip()

    if not id:
        resp = {
            'success': False,
            'msg': 'invalid resume id'
        }

        response = make_response(jsonify(resp))
        return response

    # resume = qushu_resume_coll.find_one({'_id': ObjectId(id)})

    # resume_url = zhilian.search(resume)
    # print('resume_url:', resume_url)

    # resp = {
    #     'success': True,
    #     'data': {
    #         'resume_url': resume_url
    #     }
    # }

    data = zhilian.login('sousuozhanghao2', 'zhaopin123')

    resp = {
        'success': True,
        'data': data
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/resume_info.do', methods = ['GET'])
def resume_info():
    if not 'username' in session:
        return redirect(url_for('login'))

    id = request.args.get('id', '').strip()

    if not id:
        resp = {
            'success': False,
            'msg': 'invalid resume id'
        }

        response = make_response(jsonify(resp))
        return response

    resume = qushu_resume_coll.find_one({'_id': ObjectId(id)}, {'_id': 0})

    resp = {
        'success': True,
        'data': resume
    }

    response = make_response(jsonify(resp))
    return response
